import { observable, action, autorun, toJS } from 'mobx';
import * as  Apis from '../apis/govern';
import {message} from 'antd';
import { ModelStore } from './modelStore';
import { ListStore } from './listStore';

export class GovernMindStore {

    @observable mindData = {};
    @observable nodeList = {};
    @observable loaded = "";
    @observable params = {};
    @observable rightMap = {};
    @observable classid = [];
    _jm;
    @observable loading = true;
    /**
     * 前端拖动回调方法
     */
    @action
    saveDragendMind = (arr, old_parent, new_parent, selectd_node) => {
        this.editable();
        this.initMap();
    }

    @action
    jsmindLoading = (end) => {
        if (end) {
            this.initMap();
        }
    }
//var data = {"id":"8","topic":"督办工作立项20180101-001","isroot":true,"children":[{"topic":"<table class=\"DBcard\"  skey=\"9\" reportLevel=\"0\" splitLevel=\"0\" disLevel=\"1\" changeLevel=\"0\"  onclick='clickTable(\"9\")'><tr><td valign=\"top\"; style=\"width:20px;\"><img style=\"width:20px;\" src=\"/messager/images/icon_m_wev8.jpg\" ></td><td style=\"padding:0px 2px;\"><table class=\"mind\" style=\"width:100%;\"><tr class=\"mind\" valign=\"top\";><td><a onclick='openTask(\"9\",\"11111\")' title='11111'>11111</a></td><td></td><td style=\"text-align: right;\"><span  style=\"background-color:#00a3ff;color:#FFFFFF;border-radius: 15px;font-size:11px;font-weight:600;padding:0 10px;display:inline-block;width:auto;height:24px;line-height:24px;text-align:center; \">完成</span></td></tr><tr style=\"height:20px;\"><td colspan=3  style=\"word-break:break-all;white-space:normal;width:185px;\"><span title='11111' style=\"display:inline-block;overflow:hidden;\">11111</span></td></tr><tr><td style=\"padding:0px 5px;\" height=\"1px;\"; bgcolor=\"#e7e7e7\"; colspan='3'></td></tr><tr><table><tr style=\"height:20px;\"><td><span style=\"width:128px;display:inline-block;overflow:hidden;\"><img style=\"width:13px;vertical-align:middle;\"src=\"/govern/image/imcode.png\">主办：<a href=\"javascript:openHrmResourceView(15)\">李丹</a></span></td></tr><tr><td><span style=\"width:128px;display:inline-block;overflow:hidden;\"><img style=\"width:13px;vertical-align:middle;\"src=\"/govern/image/imcode.png\">协办：<a href=\"javascript:openHrmResourceView(10)\">郭靖</a>,<a href=\"javascript:openHrmResourceView(16)\">张华</a></span></td></tr></table></tr></table></td></tr></table>","id":"9"},{"topic":"<table class=\"DBcard\"  skey=\"10\" reportLevel=\"0\" splitLevel=\"0\" disLevel=\"1\" changeLevel=\"0\"  onclick='clickTable(\"10\")'><tr><td valign=\"top\"; style=\"width:20px;\"><img style=\"width:20px;\" src=\"/messager/images/icon_m_wev8.jpg\" ></td><td style=\"padding:0px 2px;\"><table class=\"mind\" style=\"width:100%;\"><tr class=\"mind\" valign=\"top\";><td><a onclick='openTask(\"10\",\"22222\")' title='22222'>22222</a></td><td></td><td style=\"text-align: right;\"><span  style=\"background-color:#00a3ff;color:#FFFFFF;border-radius: 15px;font-size:11px;font-weight:600;padding:0 10px;display:inline-block;width:auto;height:24px;line-height:24px;text-align:center; \">完成</span></td></tr><tr style=\"height:20px;\"><td colspan=3  style=\"word-break:break-all;white-space:normal;width:185px;\"><span title='22222' style=\"display:inline-block;overflow:hidden;\">22222</span></td></tr><tr><td style=\"padding:0px 5px;\" height=\"1px;\"; bgcolor=\"#e7e7e7\"; colspan='3'></td></tr><tr><table><tr style=\"height:20px;\"><td><span style=\"width:128px;display:inline-block;overflow:hidden;\"><img style=\"width:13px;vertical-align:middle;\"src=\"/govern/image/imcode.png\">主办：<a href=\"javascript:openHrmResourceView(9)\">姚丹</a></span></td></tr></table></tr></table></td></tr></table>","id":"10"}]};
    /**
     * 初始化思维导图
     */
    initMap = () => {
        if (this.loaded == "1" && window.jsMind) {
            try{
                jQuery("#jsmind_container").html("");
            }catch(e){}
            var options = {
                container: 'jsmind_container',
                editable: false,
                theme: 'weaver',
                support_html: true,
                layout: {
                    hspace: 150,         // 节点之间的水平间距
                    vspace: 15,          // 节点之间的垂直间距
                    pspace: 13           // 节点收缩/展开控制器的尺寸
                }
            }
            this._jm = new window.jsMind(options);

            var mind = {
                meta: {},
                format: 'node_tree',
                data: toJS(this.mindData)
            };
            this._jm.show(mind);
            //this.editable();
            document.onclick = function () {
                jQuery("#menu").css("width", "0px");
                jQuery("#menu").css("border", 'solid 0px #666');
                
            };
            this.classid.forEach((item,index)=>{
                jQuery(".governimg_"+item).hover(function(){
                    jQuery(".governTip_"+item).css({'display':'block',
                    'position':'absolute','top':'11px','right':'19%',
                    'width':'49px','border':'solid','background-color':'#636363',
                    'padding':'3px','color':'#fff','z-index':'12','text-align':'center'})
                },function(){
                    jQuery(".governTip_"+item).css("display","none");
                });
            })
            
            // jQuery("#governTip").css({'display':'block','position':'absolute','top':'12px','left':'9px','width':'15em','border':'1px solid black','background-color':'#ccFFFF','padding':'3px','color':'black'})
            jQuery("[nodeid='"+this.params.id+"']").css("border","1px #FF0000 solid");//对进度视图中的当前节点  添加额外的红框
        }
    }

    /**
     * 调整拖拽控制级别
     */
    @action
    editable = () => {
        this._jm.enable_edit();
    }

    /**
     * 放大
     */
    @action
    zoomIn = () => {
        if (this._jm) {
            this._jm.view.zoomIn();
        }
    }

    /**
     * 缩小
     */
    @action
    zoomOut = () => {
        if (this._jm) {
            this._jm.view.zoomOut();
        }
    }

    /**
     * 收缩
     */
    @action
    collapse_all = () => {
        if (this._jm) {
            this._jm.collapse_all();
        }
    }

    /**
     * 展开
     */
    @action
    expand_all = () => {
        if (this._jm) {
            this._jm.expand_all();
        }
    }

    /**
     * 页面初始化
     */
    @action
    init = (params) => {
        this.loading = true;
        this.params = params;
        this.taskid = params.id;
        Apis.initMind(params).then((data) => {
            if(parseInt(data.status) == 1){
              this.rightMap = data.rightMap;
              this.mindData = data.mindMap;
              this.loaded = data.loaded;
              this.classid = data.classid;
              window.saveDragendMind = this.saveDragendMind;
              window.clickTable = this.clickTable;
              window.openTask = this.openTask;
              window.openTask2 = this.openTask;
              this.initMap();
            }else{
              message.error(data.error);
            }
            this.loading = false;
        }).catch(error => {
            message.error(error);
        });
    }

    /**
     * 前端点击区块的回调事件  非默认展开子节点加载入口
     */
    @action
    clickTable = (id) => {
        this._jm.toggle_node(id);//节点收缩
    }
    @action
    openTask = (id, text) => {
      //19.03.29 lusx 点击打开新窗口不让同时执行收缩
      let event = window.event || arguments[0];
      if (event.stopPropagation) {
        event.stopPropagation();
      } else {
        event.cancelBubble = true;
      }
      //const url = "/spa/govern/static/index.html#/main/govern/proinfo?id="+id;
      // const url = "/#/main/govern/proinfo?id="+id;
      //方法1--新建a标签
      // var a = document.createElement('a');
      // a.setAttribute('href', url);
      // a.setAttribute('target', '_self');
      // a.setAttribute('id', id);
      // // 防止反复添加
      // if(!document.getElementById(id)) {
      //     document.body.appendChild(a);
      // }
      // a.click();
      // document.body.removeChild(a);

      //方法2--
      var form = document.createElement('form');
      form.action = "/spa/govern/static/index.html#/main/govern/proinfo?id="+id;
      form.target = '_blank';
      //form.method = 'POST';
      document.body.appendChild(form);
      form.submit();
      document.body.removeChild(form);
      //方法3--
      // var tempwindow=window.open('_blank');
      // tempwindow.location="/spa/govern/static/index.html#/main/govern/proinfo?id="+id;
      //方法4--
      //hrefTaskInfo(id);
  };

}
